unit MLPAShowSamples;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ExtCtrls, TeEngine, Series, TeeProcs, Chart, Grids, DBCtrls;

type
  TdlgShowAnalysisSample = class(TForm)
    OKBtn: TButton;
    CancelBtn: TButton;
    Bevel1: TBevel;
    Chart1: TChart;
    Series1: TPointSeries;
    Series2: TPointSeries;
    Series3: TPointSeries;
    Series4: TPointSeries;
    Label1: TLabel;
    lblSampleId: TLabel;
    Label2: TLabel;
    lblReplicateNr: TLabel;
    Series5: TPointSeries;
    rgPlotLabels: TRadioGroup;
    sgSamples: TStringGrid;
    cbAdjustScale: TCheckBox;
    Label4: TLabel;
    dbtTechnician: TDBText;
    dbtDate: TDBText;
    Label3: TLabel;
    procedure rgPlotLabelsClick(Sender: TObject);
    procedure cbAdjustScaleClick(Sender: TObject);
  private
    { Private declarations }
    CurrentMinimum:Real;
    CurrentMaximum:Real;
    CurrentSamples:array of integer;
    procedure SetLabels;
    procedure CalculateMiniMax;
    procedure SetSeries;
  public
    { Public declarations }
    procedure DoDialog(samples:array of integer);
  end;

var
  dlgShowAnalysisSample: TdlgShowAnalysisSample;

implementation
uses MLPAOptions,MLPAMain,MLPAdm, Math,IEEE754;

{$R *.DFM}

{ TdlgShowAnalysisSample }

procedure TdlgShowAnalysisSample.DoDialog;
var
  i,j:integer;

begin
  // fill in data of selected sample
  lblSampleId.Caption:=frmMain.sgAnalyses.Cells[asgLabId,samples[0]];
  lblReplicateNr.Caption:=IntToStr(Length(Samples));
  // Ticket #4 (1-apr-2006)
  sgSamples.RowCount:=1+Length(Samples);
  sgSamples.Cells[0,0]:='Sample';
  sgSamples.Cells[1,0]:='Q1';
  sgSamples.Cells[2,0]:='Q2';
  sgSamples.Cells[3,0]:='Q3';
  SetLength(CurrentSamples,length(samples));
  for i:=low(samples) to high(samples) do
    CurrentSamples[i]:=samples[i];
  CalculateMiniMax;
  for i:= 0 to Chart1.SeriesCount -1 do
  with Chart1.Series[ i ] do
  begin
    Clear;
    if i<=High(samples) then
    begin
      Title:=frmMain.sgAnalyses.Cells[asgAssayId,samples[i]];
      // Sample info
      sgSamples.Cells[0,i+1]:=frmMain.sgAnalyses.Cells[asgAssayId,samples[i]];
      for j:=0 to 2 do
      begin
        if isnan(frmMain.QCscore[j,samples[i]]) then
          sgSamples.Cells[j+1,i+1]:=''
        else
          sgSamples.Cells[j+1,i+1]:=FormatFloat('0.#',frmMain.QCscore[j,samples[i]-1])
      end;
      Active:=true;
    end
    else begin
      Active:=false;
    end;
  end;
  SetSeries;
  SetLabels;
  ShowModal;
end;

procedure TdlgShowAnalysisSample.SetLabels;
var
  acol:integer;
  ProbeInfo:TProbeInfo;
begin
  for acol:=frmMain.startdata to frmMain.sgAnalyses.ColCount-1 do
  begin
    ProbeInfo:=TProbeInfo(frmMain.datalist.Objects[frmMain.ProbeSortIndex[ACol-frmMain.startdata]]);
    case rgPlotLabels.ItemIndex of
      0:Chart1.Series[0].Xlabel[acol-frmMain.Startdata]:=ProbeInfo.id;
      1:Chart1.Series[0].Xlabel[acol-frmMain.Startdata]:=ProbeInfo.probe;
      2:Chart1.Series[0].Xlabel[acol-frmMain.Startdata]:=ProbeInfo.gene;
      3:Chart1.Series[0].Xlabel[acol-frmMain.Startdata]:=ProbeInfo.genesub;
      4:Chart1.Series[0].Xlabel[acol-frmMain.Startdata]:=ProbeInfo.chrpos;
      5:Chart1.Series[0].Xlabel[acol-frmMain.Startdata]:=ProbeInfo.cytoband;
    end;
  end;

end;

procedure TdlgShowAnalysisSample.rgPlotLabelsClick(Sender: TObject);
begin
  SetLabels;
end;

procedure TdlgShowAnalysisSample.cbAdjustScaleClick(Sender: TObject);
begin
  SetSeries;
end;

procedure TdlgShowAnalysisSample.CalculateMiniMax;
var
  i,acol:integer;
  value:string;
begin
  CurrentMinimum:=100;
  CurrentMaximum:=0;
  for i:= 0 to Chart1.SeriesCount -1 do
  with Chart1.Series[ i ] do
  begin
    if i<=High(CurrentSamples) then
    begin
      for acol:=frmMain.startdata to frmMain.sgAnalyses.ColCount-1 do
      begin
        value:=frmMain.sgAnalyses.Cells[acol,CurrentSamples[i]];
        if value<>'' then
        begin
          CurrentMinimum:=Min(CurrentMinimum,StrToFloat(value));
          CurrentMaximum:=Max(CurrentMaximum,StrToFloat(value));
        end;
      end;
    end;
  end;
end;

procedure TdlgShowAnalysisSample.SetSeries;
var
  i,acol:integer;
  value:string;
begin
  If cbAdjustScale.Checked then
  begin
    Chart1.LeftAxis.Minimum:=CurrentMinimum-0.05;
    Chart1.LeftAxis.Maximum:=CurrentMaximum+0.05;
  end
  else begin
    Chart1.LeftAxis.Minimum:=0.05;
    Chart1.LeftAxis.Maximum:=2.45;
  end;
  for i:= 0 to Chart1.SeriesCount -1 do
  with Chart1.Series[ i ] do
  begin
    Clear;
    if i<=High(CurrentSamples) then
    begin
      for acol:=frmMain.startdata to frmMain.sgAnalyses.ColCount-1 do
      begin
        value:=frmMain.sgAnalyses.Cells[acol,CurrentSamples[i]];
        if value<>'' then
        begin
          AddXY(acol-frmMain.Startdata+1,Min(Max(StrToFloat(value),Chart1.Leftaxis.Minimum),Chart1.Leftaxis.Maximum));
        end
        else begin
          //OostDev bug#:1 Create dummy spot below y-axis if it is empty value
          AddXY(acol-frmMain.Startdata+1, -1);
        end;
      end;
    end;
  end;
  SetLabels;
end;

end.
